package org.eenie.core.base.viewmodel

import androidx.lifecycle.ViewModel
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.*

open class BaseViewModel<VS : Any>(initialState: VS) : ViewModel() {

    private var _state: MutableSharedFlow<VS> = MutableSharedFlow(1, 0, onBufferOverflow = BufferOverflow.DROP_OLDEST)
    var state: SharedFlow<VS> = _state


    private var lastState: VS = initialState

    protected var viewState: VS
        get() = lastState
        set(value) {
            lastState = value
            _state.tryEmit(value)
        }


}